// config-overrides.js
const package = require('./package.json');
module.exports = {
    webpack: function (config, env) {
        if (env === "production") {
            let packageVersion = package.version;
            console.log(`应用包版本： ${packageVersion}`);
            console.log("替换前js输出配置", config.output);

            //JS Overrides
            config.output.filename = `static/js/[name]_${packageVersion}.js`;
            config.output.chunkFilename = `static/js/[name]_${packageVersion}.chunk.js`;
            console.log("替换后js输出配置", config.output);

            //CSS Overrides
            for (let plugin of config.plugins) {
                if (plugin.constructor.name === "MiniCssExtractPlugin") {
                    console.log("替换前Css配置", plugin);
                    plugin.options.filename = `static/css/[name]_${packageVersion}.css`;
                    plugin.options.chunkFilename = `static/css/[name]_${packageVersion}.css`;

                    console.log("替换后Css配置", plugin);
                }
            }

            //Media Overrides
            config.module.rules[1].oneOf[0].options.name = `static/media/[name]_${packageVersion}.[ext]`;
            config.module.rules[1].oneOf[1].options.name = `static/media/[name]_${packageVersion}.[ext]`;

            let oneOfCount = config.module.rules[1].oneOf.length;
            let lastConfigOfOneOf = config.module.rules[1].oneOf[oneOfCount - 1];
            lastConfigOfOneOf.options.name = `static/media/[name]_${packageVersion}.[ext]`;

            console.log("替换后的媒体配置", config.module.rules[1].oneOf[0], config.module.rules[1].oneOf[1], lastConfigOfOneOf);
        }

        return config;
    }
};
